package com.hxm.demo.newcoder;

public class TestBM4 {

    public static void main(String[] args) {
        ListNode pHead1 = new ListNode(1);
        pHead1.next = new ListNode(3);
        pHead1.next.next = new ListNode(5);

        ListNode pHead2 = new ListNode(1);
        pHead2.next = new ListNode(3);
        pHead2.next.next = new ListNode(5);
        ListNode listNode = Merge2(pHead1, pHead2);
        System.out.println(listNode);
    }

    public static ListNode Merge1(ListNode pHead1, ListNode pHead2) {
        // write code here
        if (pHead1 == null) {
            return pHead2;
        }
        if (pHead2 == null) {
            return pHead1;
        }
        ListNode pre = new ListNode(-1);
        ListNode merge = pre;
        while (pHead1 != null) {
            while (pHead2 != null) {
                if (pHead1.val >= pHead2.val) {
                    pre.next = pHead2;
                    pre = pre.next;
                    pHead2 = pHead2.next;
                } else {
                    pre.next = pHead1;
                    pre = pre.next;
                    break;
                }
            }
            if (pHead2 == null) {
                break;
            }
            pHead1 = pHead1.next;
        }
        if (pHead1 != null) {
            pre.next = pHead1;
        }
        if (pHead2 != null) {
            pre.next = pHead2;
        }
        return merge.next;
    }

    public static ListNode Merge2(ListNode pHead1, ListNode pHead2) {
        // write code here
        if (pHead1 == null) {
            return pHead2;
        }
        if (pHead2 == null) {
            return pHead1;
        }
        if (pHead1.val >= pHead2.val) {
            pHead2.next = Merge2(pHead1, pHead2.next);
            return pHead2;
        } else {
            pHead1.next = Merge2(pHead1.next, pHead2);
            return pHead1;
        }
    }
}
